<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>

    var nextLargerNodes = function (head) {
      let ans = [];
      let dfs = (link) => {
        if (!link) return;
        let other = link;
        while (other.next && link.val >= other.next.val) { //等于号是关键
          other = other.next;
        }
        ans.push(other.next?.val > link.val ? other.next?.val : 0);
        dfs(link.next)
      }
      dfs(head);
      return ans;
    };
    console.log(nextLargerNodes(head = [2, 1, 5]))
  </script>
</body>

</html>